﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Wheels
{
    // 此 ActionTextWriter 思路源于博文：http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers
    public sealed class ActionTextWriter : TextWriter
    {
#pragma warning disable 1591

        private readonly Action<string> mAction;

        public ActionTextWriter(Action<string> action)
        {
            this.mAction = action;
        }

        public override void Write(char[] buffer, int index, int count)
        {
            this.Write(new string(buffer, index, count));
        }
        public override void WriteLine(char[] buffer, int index, int count)
        {
            this.WriteLine(new string(buffer, index, count));
        }

        public override void Write(char value)
        {
            this.Write(new string(value, 1));
        }

        public override void Write(string value)
        {
            this.mAction.Invoke(value);
        }

        public override void WriteLine()
        {
            this.Write(this.NewLine);
        }
        public override void WriteLine(string value)
        {
            this.Write(value + this.NewLine);
        }

        #region WriteLine

        public override void WriteLine(bool value)
        {
            this.WriteLine(value ? "True" : "False");
        }
        public override void WriteLine(char value)
        {
            this.WriteLine(new string(value, 1));
        }
        public override void WriteLine(char[] buffer)
        {
            this.WriteLine(new string(buffer));
        }
        public override void WriteLine(decimal value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        public override void WriteLine(double value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        public override void WriteLine(float value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        public override void WriteLine(int value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        public override void WriteLine(long value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        [CLSCompliant(false)]
        public override void WriteLine(uint value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }
        [CLSCompliant(false)]
        public override void WriteLine(ulong value)
        {
            this.WriteLine(value.ToString(this.FormatProvider));
        }

        #endregion

        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }
    }
}
